export function getCustomNavData() {
	let result = {};
	uni.getSystemInfo({
		success(e) {
			let myStatusBar = 0;
			let myCustomBar = 0;
			let myNvaMartom = 0;
			let myNavHeight = 0;
			let myNavWidth = 0;
			let custom = uni.getMenuButtonBoundingClientRect();
			myStatusBar = e.statusBarHeight;
			myNavHeight = custom.height;
			myNavWidth = custom.width;
			myNvaMartom = custom.top - e.statusBarHeight;
			myCustomBar = (myNvaMartom * 2 + custom.height) - 2;
			result.statusBar = myStatusBar * 750 / wx.getSystemInfoSync().windowWidth; //顶部状态栏高度
			result.customBar = (myCustomBar * 750 / wx.getSystemInfoSync().windowWidth) + 12; //导航栏高度
			result.nvaMartom = myNvaMartom * 750 / wx.getSystemInfoSync().windowWidth; //胶囊上边距
			result.navHeight = myNavHeight * 750 / wx.getSystemInfoSync().windowWidth; //胶囊按钮高度
			result.navWidth = myNavWidth * 750 / wx.getSystemInfoSync().windowWidth; //胶囊按钮宽度
			result.navMarRight = 750 - (custom.right * 750 / wx.getSystemInfoSync().windowWidth); //胶囊右边距
			result.pageMagTop = result.customBar + result.statusBar; //页面内容距离顶部的上边距，导航栏全部高度
		}
	})
	return result
}
export function getSalaryData(select, step = 1000, num = 80) {
	let start = [];
	let end = [];
	for (let i = 1; i <= num; i++) {
		let text = i * step;
		start.push(text);
		if (select && select + 5 >= i && i >= (select + 1)) {
			end.push(text)
		}
	}
	if (select == num) {
		end.push(select * step)
	}
	return [start, end];
}
//处理多选数组
export function multipleChoiceChange(arr, value, max = -1) {
	if (!arr.includes(value)) {
		if (max == -1 || arr.length < max) {
			arr.push(value)
		} else {
			uni.showToast({
				title: `最多只能选择${max}个`,
				icon: 'none'
			})
		}
	} else {
		const index = arr.indexOf(value)
		arr.splice(index, 1)
	}
	return arr;
}
//处理多选对象
export function multipleChoiceChangeObj(targetName, value, vm, max = -1) {
	const data = vm[targetName]
	if (!data[value.id]) {
		if (max == -1 || Object.keys(data).length < max) {
			vm.$set(vm[targetName], value.id, value)
		} else {
			uni.showToast({
				title: `最多只能选择${max}个`,
				icon: 'none'
			})
		}
	} else {
		vm.$delete(vm[targetName], value.id)
	}
}
export function isMore(page, limit, total) {
	return page * limit < total;
}
export function showDevelopingTips() {
	uni.showToast({
		title: '该功能暂未开放',
		icon: 'none'
	})
}
export function getCurrentTime(fields = 's', time = null) {
	const date = time ? new Date(time) : new Date();
	let str = '';
	let y = date.getFullYear().toString();
	let m = (date.getMonth() + 1).toString();
	m = m.length == 1 ? '0' + m : m
	let d = date.getDate().toString();
	d = d.length == 1 ? '0' + d : d
	let h = date.getHours().toString();
	h = h.length == 1 ? '0' + h : h
	let i = date.getMinutes().toString();
	i = i.length == 1 ? '0' + i : i
	let s = date.getSeconds().toString();
	s = s.length == 1 ? '0' + s : s
	if (fields == 'y') {
		str = y;
	}
	if (fields == 'm') {
		str = y + '-' + m
	}
	if (fields == 'd') {
		str = y + '-' + m + '-' + d
	}
	if (fields == 'h') {
		str = y + '-' + m + '-' + d + ' ' + h
	}
	if (fields == 'i') {
		str = y + '-' + m + '-' + d + ' ' + h + ':' + i
	}
	if (fields == 's') {
		str = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
	}
	return str;
}
export function tsToTime(timestamp, relative = false) {
	if (typeof timestamp === 'string') {
		timestamp = Number(timestamp);
	}
	timestamp *= 1000
	if (typeof timestamp !== 'number') {
		alert("输入参数无法识别为时间戳");
	}
	let date = new Date(timestamp);
	let Y = date.getFullYear() + '年';
	let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '月';
	let D = date.getDate().toString();
	D = D.length == 1 ? '0' + D : D
	D = D + '日 '
	let h = date.getHours().toString();
	h = h.length == 1 ? '0' + h : h
	h = h + ':'
	let m = date.getMinutes().toString();
	m = m.length == 1 ? '0' + m : m
	m = m + ':'
	let s = date.getSeconds().toString();
	s = s.length == 1 ? '0' + s : s
	return Y + M + D + h + m + s;
}
export function showToast(msg, callback = function() {}) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 2000,
		mask: true,
		success(e) {
			setTimeout(function() {
				callback()
			}, 2000)
		}
	})
}
export function SplitArray(list, sp) {
	if (typeof list != 'object') return [];
	if (sp === undefined) sp = [];
	for (var i = 0; i < list.length; i++) {
		sp.push(list[i]);
	}
	return sp;
}
export function getMediaAuth() {
	return [
		new Promise((resolve, reject) => {
			uni.authorize({
				scope: 'scope.record',
				success() {
					resolve()
				},
				fail() {
					reject()
				}
			})
		}),
		new Promise((resolve, reject) => {
			uni.authorize({
				scope: 'scope.camera',
				success() {
					resolve()
				},
				fail() {
					reject()
				}
			})
		})
	]
}
export function getYearsAgo(year) {
	const date = new Date();
	date.setFullYear(date.getFullYear() - year);
	return getCurrentTime('d', date);
}
export function getPickerIndex(selected, data, field = 'id') {
	for (let i = 0; i < data.length; i++) {
		if (selected == data[i][field]) {
			return i;
		}
	}
}
export function get24HoursTime(time) {
	if (time.indexOf('上午') != -1) {
		time = time.replace('上午', '');
		let arr = time.split(':');
		arr[0] = arr[0] == '12' ? '00' : arr[0];
		arr[0] = arr[0].length == 2 ? arr[0] : '0' + arr[0];
		time = arr.join(':')
	}
	if (time.indexOf('下午') != -1) {
		time = time.replace('下午', '');
		let arr = time.split(':');
		arr[0] = arr[0] == '12' ? arr[0] : arr[0] * 1 + 12;
		arr[0] = arr[0] == '24' ? '00' : arr[0].toString()
		arr[0] = arr[0].length == 2 ? arr[0] : '0' + arr[0];
		time = arr.join(':')
	}
	return time;
}

export function getScene(scene) {
	let param = {}
	scene = decodeURIComponent(scene);
	if (scene) {
		var scene_arr = scene.split('&');
		var arr = [];
		for (let i = 0; i < scene_arr.length; i++) {
			var chaifen = scene_arr[i].split('=');
			param[chaifen[0]] = chaifen[1];
		}
	}
	return param;
}
export function throttle(fn, wait) {
	var pre = Date.now();
	return function() {
		var context = this;
		var args = arguments;
		var now = Date.now();
		if (now - pre >= wait) {
			fn.apply(context, args);
			pre = Date.now();
		}
	}
}
export function getAuthorize(scopeName, authorizeName) {
	return new Promise((resolve, reject) => {
		uni.authorize({
			scope: scopeName,
			success: () => {
				resolve()
			},
			fail: () => {
				uni.showModal({
					title: '提示',
					content: '您拒绝了' + authorizeName + '授权，请前往设置页开启',
					confirmText: '去开启',
					success(e) {
						if (e.confirm) {
							uni.openSetting()
						}
					}
				})
				reject()
			},
		})
	})

}
export function getLocation() {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			isHighAccuracy: true,
			geocode: true,
			success(e) {
				resolve(e)
			},
			fail(e) {
				reject()
			}
		})
	})
}
export default {
	getCustomNavData,
	showDevelopingTips
}